package com.springdoor.goods.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.springdoor.common.annotation.DataScope;
import com.springdoor.common.utils.DateUtils;
import com.springdoor.common.utils.spring.SpringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.springdoor.goods.mapper.SprdGoodsShopMapper;
import com.springdoor.goods.domain.SprdGoodsShop;
import com.springdoor.goods.service.ISprdGoodsShopService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 店铺商品关联Service业务层处理
 *
 * @author llshs
 * @date 2024-12-04
 */
@Service
public class SprdGoodsShopServiceImpl implements ISprdGoodsShopService
{
    @Autowired
    private SprdGoodsShopMapper sprdGoodsShopMapper;

    /**
     * 查询店铺商品关联
     *
     * @param gshopId 店铺商品关联主键
     * @return 店铺商品关联
     */
    @Override
    public SprdGoodsShop selectSprdGoodsShopByGshopId(Long gshopId)
    {
        return sprdGoodsShopMapper.selectSprdGoodsShopByGshopId(gshopId);
    }

    /**
     * 查询店铺商品关联列表
     *
     * @param sprdGoodsShop 店铺商品关联
     * @return 店铺商品关联
     */
    @Override
    @DataScope(deptAlias = "d")
    public List<SprdGoodsShop> selectSprdGoodsShopList(SprdGoodsShop sprdGoodsShop)
    {
        return sprdGoodsShopMapper.selectSprdGoodsShopList(sprdGoodsShop);
    }

    /**
     * 新增店铺商品关联
     *
     * @param sprdGoodsShop 店铺商品关联
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSprdGoodsShop(SprdGoodsShop sprdGoodsShop)
    {
        int row = 0;
        Map<String, Object> params = sprdGoodsShop.getParams();
        if (params != null && !params.isEmpty()) {
            List<String> gNumbers = (List<String>) params.get("gNumbers");
            if (gNumbers != null && !gNumbers.isEmpty()) {
                Integer orderNum = sprdGoodsShop.getOrderNum();
                if (orderNum == null || orderNum <= 0) {
                    orderNum = sprdGoodsShopMapper.findMaxOrderNum(sprdGoodsShop.getDeptId());
                }
                for (String gNumber : gNumbers) {
                    if (StringUtils.isNotBlank(gNumber)) {
                        sprdGoodsShop.setgNumber(gNumber);
                        sprdGoodsShop.setOrderNum(orderNum++);
                        row += sprdGoodsShopMapper.insertSprdGoodsShop(sprdGoodsShop);
                    }
                }
            }
        }
        return row;
    }

    /**
     * 修改店铺商品关联
     *
     * @param sprdGoodsShop 店铺商品关联
     * @return 结果
     */
    @Override
    public int updateSprdGoodsShop(SprdGoodsShop sprdGoodsShop)
    {
        return sprdGoodsShopMapper.updateSprdGoodsShop(sprdGoodsShop);
    }

    /**
     * 批量删除店铺商品关联
     *
     * @param gshopIds 需要删除的店铺商品关联主键
     * @return 结果
     */
    @Override
    public int deleteSprdGoodsShopByGshopIds(Long[] gshopIds)
    {
        return sprdGoodsShopMapper.deleteSprdGoodsShopByGshopIds(gshopIds);
    }

    /**
     * 删除店铺商品关联信息
     *
     * @param gshopId 店铺商品关联主键
     * @return 结果
     */
    @Override
    public int deleteSprdGoodsShopByGshopId(Long gshopId)
    {
        return sprdGoodsShopMapper.deleteSprdGoodsShopByGshopId(gshopId);
    }

    @Override
    public Integer findMaxOrderNum(Long deptId) {
        return sprdGoodsShopMapper.findMaxOrderNum(deptId);
    }

    @Override
    @DataScope(deptAlias = "d")
    public List<Map<String, Object>> selectShopGoodsTypeNum(SprdGoodsShop sprdGoodsShop) {
        return sprdGoodsShopMapper.selectShopGoodsTypeNum(sprdGoodsShop);
    }

    @Override
    public JSONObject getShopGoodsTypeNum() {
        SprdGoodsShop sprdGoodsShop = new SprdGoodsShop();
        List<Map<String, Object>> list = SpringUtils.getAopProxy(this).selectShopGoodsTypeNum(sprdGoodsShop);
        if (list == null || list.isEmpty()) {
            return null;
        }
        JSONObject options = new JSONObject();
        JSONArray legendData = new JSONArray();
        JSONArray xAxisData = new JSONArray();
        JSONArray series = new JSONArray();

        Set<String> deptSet = new HashSet();
        Set<String> typeSet = new HashSet();
        List<String> deptList = new ArrayList<>();
        List<String> typeList = new ArrayList<>();

        for (Map<String, Object> map: list) {
            String deptName = (String) map.get("deptName");
            String typeName = (String) map.get("typeName");
            deptSet.add(deptName);
            typeSet.add(typeName);
        }
        for (String typeName: typeSet) {
            legendData.add(typeName);
        }
        for (String deptName: deptSet) {
            xAxisData.add(deptName);
        }
        for (String category : typeSet) {
            JSONObject seriesItem = new JSONObject();
            seriesItem.put("name", category);
            seriesItem.put("type", "bar");
            JSONArray values = new JSONArray();

            for (String store : deptSet) {
                Integer count = 0;
                for (int i = 0, len = list.size(); i < len; i++) {
                    if (list.get(i).get("deptName").equals(store) && list.get(i).get("typeName").equals(category)) {
                        count = ((Long) list.get(i).get("count")).intValue();
                        i = len;
                    }
                }
                values.add(count);
            }

            seriesItem.put("data", values);
            series.add(seriesItem);
        }
        options.put("title", new JSONObject().fluentPut("text", "店铺商品统计"));
        options.put("legend", new JSONObject().fluentPut("data", legendData));
        options.put("xAxis", new JSONObject().fluentPut("data", xAxisData));
        options.put("yAxis", new JSONObject());
        options.put("series", series);
        return options;
    }

    @Override
    public int putOnShops(Map<String, Object> map) {
        return sprdGoodsShopMapper.putOnShops(map);
    }

    @Override
    public SprdGoodsShop selectGoodsMainByDeptGnumber(SprdGoodsShop sprdGoodsShop) {
        return sprdGoodsShopMapper.selectGoodsMainByDeptGnumber(sprdGoodsShop);
    }

}
